﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;


namespace CMS.BLL
{
    public class Basket
    {
          #region Common
        public eBasket   Data;

        public Basket()
        {
            Data = new eBasket();
		}

        ~Basket()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cBasket objDAL = new cBasket();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cBasket objDAL = new cBasket();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cBasket objInfor = new cBasket(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.BasketID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cBasket objInfor = new cBasket();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cBasket objDAL = new cBasket();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.BasketID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cBasket objInfor)
        {
            objInfor.BasketName = Data.BasketName;
            objInfor.CustomerName = Data.CustomerName;
            objInfor.CustomerAddress = Data.CustomerAddress;
            objInfor.CustomerTel = Data.CustomerTel;
            objInfor.CustomerEmail = Data.CustomerEmail;
            objInfor.Status = Data.Status;
            objInfor.DateCreated = Data.DateCreated;
            objInfor.BasketContent = Data.BasketContent;
            objInfor.ContactOfficeID = Data.ContactOfficeID; 
            
        }

        private void ConvertData(cBasket objInfor)
        {
            Data.BasketName=objInfor.BasketName  ;
            Data.CustomerName =objInfor.CustomerName ;
            Data.CustomerAddress =objInfor.CustomerAddress  ;
            Data.CustomerTel =objInfor.CustomerTel  ;
            Data.CustomerEmail =objInfor.CustomerEmail ;
            Data.Status  =objInfor.Status  ;
            Data.DateCreated =objInfor.DateCreated  ;
            Data.BasketContent = objInfor.BasketContent;
             Data.ContactOfficeID= objInfor.ContactOfficeID ;
        }
        #endregion

        public DataTable Search(int ItemView, string Title,  int Status, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cBasket objDAL = new cBasket();
            tbResult = objDAL.Search( ItemView,  Title,   Status,   PageIndex,  PageSize, ref  PageCount );
            return tbResult;
        }
    }
}
